home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / BUS / BibTeX 1.06 FAT.sit / BibTeX ƒ / Source code / bibext.cc next >
C/C++ Source or Header  |  1996-05-17  |  9KB  |  390 lines

  1. /*
  2.  * Auxilliary routines for BibTeX in C.
  3.  *
  4.  * Tim Morgan 2/15/88
  5.  * Eduardo Krell 4/21/88
  6.  * Vince Darley 2/96
  7.  */
  8.  
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "MoreFilesExtras.h"
  12.  
  13. #define filenamesize 1024
  14.  
  15.  
  16. #include "bibutils.h"
  17. #include "BibTeX.h"
  18. #include "file-io.h"
  19. #include "CBibTeXApp.h"
  20. #include "bibext.h"
  21.  
  22. #define getc(a) GetBibtexCharacter(a)
  23. #define ungetc(a,b) UnGetC(a,b)
  24. #define feof(a) FEof(a)
  25.  
  26. static void packrealnameoffile(char ** cpp );
  27. static unsigned long GetDirIDFromFSSpec(FSSpecPtr pFSS);
  28.  
  29. static FSSpec getAppSpec(void);
  30.  
  31. /*
  32.  *  Declarations for file search 
  33.  */
  34. FSSpec        Folders[3];
  35. FInfo    AuxFileInfo;
  36.  
  37. extern char realnameoffile[];
  38. extern char xord[], *buffer, nameoffile[];    /* change buffer for dyn alloc, jws */
  39.  
  40. static char input_path[filenamesize];
  41.  
  42. /*
  43.     This important function will open a file, finding it in any of
  44.     the directories specified if required, if the file is for creating
  45.     it gives it the correct creator (same as the .aux).
  46.     
  47.     To Do:
  48.     
  49.     Although I've changed a lot here, it would be sensible to do
  50.     away with the odd scheme of find and open file, then close it
  51.     and re-open.
  52.     
  53.     Vince Feb'96
  54.     
  55.     Side effects: allocates a FileDescPtr which must be freed by
  56.     the caller (my CloseFile frees this automatically).
  57. */
  58. #ifdef STDC
  59. BIBFILE *openf(char * name ,char * mode )
  60. #else
  61. BIBFILE *openf(name ,mode )
  62. char *name, *mode;
  63. #endif
  64. {
  65. /*
  66.  *  Variables for directory search ... jdl  
  67.  */
  68.     short        refnum;
  69.     int         cFolder, nPath;
  70.     OSErr ferr;
  71.     char    **PathHandle, *PathPtr;
  72.     char    expand_name[100];
  73.     Str63   temp;
  74.  
  75.     {                            /* ###### LSC doesn't like ./ (jws) */
  76.         if ( name[0] == '.' && name[1] == '/') name += 2;
  77.     }
  78. /*
  79.  *  Directory search stuff ... jdl
  80.  */
  81.      PathHandle = (char **) GetResource('STR#', 130);    /* Path resource */
  82.      if (!PathHandle) {
  83.          FatalOSErr("Path Resource not present!!!¥n",0);
  84.      }
  85.      HLock(PathHandle);
  86.      PathPtr = *PathHandle;
  87.      // this no longer works on PPC:
  88.      //nPath = *((int *) PathPtr);        
  89.      //PathPtr += sizeof(int);
  90.      // must use this:
  91.      nPath = (int) PathPtr[1];    // # of path elements
  92.      PathPtr += 2;
  93.      
  94.      FSSpec newSpec;
  95.      
  96.      // We grab the entire path resource and scan through it ourselves.
  97.      while (nPath-- > 0) {
  98.          PtoCstr((unsigned char*) PathPtr);
  99.         cFolder = *PathPtr - '0';      // which folder:0 =current,1 = tex inputs,2=path given
  100.          if (cFolder < 0 || cFolder > 2) {
  101.              FatalOSErr("Invalid path decription %s¥n", PathPtr,0);
  102.          }
  103.          
  104.          strcpy(expand_name, &PathPtr[1]);
  105.          strcat(expand_name, name);
  106.         CtoPstr(expand_name);
  107.         CtoPstr(PathPtr);
  108.         ferr = FSMakeFSSpec(Folders[cFolder].vRefNum, Folders[cFolder].parID, 
  109.                 (unsigned char*) expand_name, &newSpec);
  110.                         
  111.         if (!ferr) 
  112.             break;
  113.         PathPtr = PathPtr + *PathPtr + 1;   /* point at the next one */
  114.     }
  115.     
  116.     HUnlock(PathHandle);
  117.     // give up the memory
  118.     ReleaseResource(PathHandle);
  119.     
  120.     if (mode[0] == 'r') {
  121.         if(ferr) {
  122.             // file should exist, we can't find it
  123.             FatalOSErr("File %s not found¥n",name,0);
  124.             //perror(name);
  125.         } else {
  126.             ferr = FSpOpenDF(&newSpec, fsRdPerm, &refnum);
  127.             if (ferr == opWrErr) {
  128.                 // already open for writing, probably an error
  129.                 FSClose(refnum);
  130.                 ferr = FSpOpenDF(&newSpec, fsRdPerm, &refnum);
  131.                 ferr = 0;
  132.             }
  133.         }
  134.     } else {
  135.         if(ferr) {
  136.             // new file, put into same folder as aux file
  137.             strcpy(expand_name, name);
  138.             CtoPstr(expand_name);
  139.             cFolder = 0;
  140.             
  141.             ferr = FSMakeFSSpec(Folders[cFolder].vRefNum, Folders[cFolder].parID, 
  142.                     (unsigned char*) expand_name, &newSpec);
  143.             if (ferr == fnfErr) {
  144.                 ferr = FSpCreate(&newSpec,AuxFileInfo.fdCreator, 'TEXT',0);
  145.             }
  146.             
  147.             if (ferr) {
  148.                 FatalOSErr("Cound not create file %s error = %d¥n", name, ferr);
  149.             }
  150.         } 
  151.         // file now exists, so overwrite
  152.         ferr = FSpOpenDF(&newSpec, fsWrPerm, &refnum);
  153.         if (ferr == opWrErr) {
  154.             // already open for writing, probably an error
  155.             FSClose(refnum);
  156.             ferr = FSpOpenDF(&newSpec, fsWrPerm, &refnum);
  157.             ferr = 0;
  158.         }
  159.         if (ferr) {
  160.             FatalOSErr("Couldn't open file %s for writing, error = %d¥n", name, ferr);
  161.         }
  162.     }
  163.     
  164.     /* We can probably do away with this opening twice nonsense */
  165.     /* (it's a relic of the old version) */
  166.     
  167.     FSClose(refnum);  /*  Yes we open the file twice....  */
  168.     
  169.     PtoCstr((unsigned char*) expand_name);
  170.     
  171.     temp[0] = sprintf((char *) temp + 1, "%s", expand_name);
  172.     FileDesc& gTempDesc = *(new FileDesc);
  173.     gTempDesc.refNum = 0;
  174.  
  175.     OSErr err = ::FSMakeFSSpec(Folders[cFolder].vRefNum, Folders[cFolder].parID, temp,
  176.                          &gTempDesc.spec);
  177.     if (err == fnfErr)
  178.         FatalOSErr("Error locating log file %s: %s.",
  179.                     (char *) temp + 1, err);
  180.     gTempDesc.spec.name[StrLength(gTempDesc.spec.name) + 1] = '¥0';
  181.     if ( mode[0] == 'r' ) {
  182.         OpenInputFile(&gTempDesc);
  183.         FSeek(&gTempDesc,0,0);
  184.     } else if ( mode[0] == 'w' ) {
  185.         OpenOutputFile(&gTempDesc);
  186.     } else {
  187.         FatalOSErr("Called open file with unknown mode; weird internal error",0);
  188.     }
  189.     
  190.     if (!&gTempDesc) {
  191.         FatalOSErr("Problem opening file %s¥n", name,0);
  192.     }
  193.  
  194.     return(&gTempDesc);
  195. }
  196.  
  197.  
  198. #ifdef STDC
  199. void lineread(BIBFILE * f ,int size )
  200. #else
  201. void lineread( f , size )
  202. BIBFILE * f;
  203. int size;
  204. #endif
  205. {
  206.     extern long last;
  207.     int in;
  208.  
  209.     last = 0;
  210.     while (last < size && (in = getc(f)) != EOF && in != '¥r' ) {
  211. #ifdef    NONASCII
  212.     buffer[last++] = xord[in];
  213. #else
  214.     buffer[last++] = in;
  215. #endif
  216.     }
  217.     while (in != EOF && in != '¥r' )    // Skip past eoln if buffer full
  218.         in = getc(f);
  219.     
  220. }
  221.  
  222.  
  223. void setpaths()
  224. {
  225.     char *p;
  226.  
  227.     // it's actually important the input_path gets a '.' put in it
  228.     if ((int)(p = getenv("TEXINPUTS")))
  229.         (void) strcpy(input_path, p);
  230.     else
  231.         (void) strcpy(input_path, TEXINPUTS);
  232.  
  233.     extern FileDesc gAuxDesc;
  234.     Folders[0] = gAuxDesc.spec;
  235.     // make this the default path in case we want to open it again
  236.     SetVol(0L, Folders[0].parID);
  237.     Folders[1] = getAppSpec();
  238.     // folders [2] requires a full path name to be specified
  239.     Folders[2].vRefNum = 0;
  240.     Folders[2].parID = 0;
  241. }
  242.  
  243.  
  244. #ifdef STDC
  245. int testaccess(int wam ,int filepath )
  246. #else
  247. int testaccess( wam , filepath )
  248. int wam, filepath;
  249. #endif
  250. {
  251.     char *path = NULL;
  252.     register int ok;
  253.  
  254.     if (filepath == 1)
  255.     path = input_path;
  256.     if (nameoffile[0] == '/')
  257.     path = NULL;
  258.     do {
  259.     packrealnameoffile(&path);
  260.     if (wam == 4)
  261.         if (access(realnameoffile, 4) == 0) ok = TRUE;
  262.         else ok = FALSE;
  263.         else {
  264. /*  fake this ... jdl
  265.         f = creat(realnameoffile, 0666);
  266.         if (f >= 0) ok = TRUE;
  267.         else ok = FALSE;
  268.         if (ok)
  269.         (void) close(f);
  270. */
  271.         ok = TRUE;
  272. /*  done ... jdl */
  273.     }
  274.     }
  275.     while (!ok && path != NULL);
  276. /*
  277.     if (ok) {
  278.     for (p=realnameoffile; *p; p++);
  279.     while (p < &(realnameoffile[filenamesize]))
  280.         *p++ = ' ';
  281.     }
  282. */
  283.     return(ok);
  284. }
  285.  
  286. #ifdef STDC
  287. static void packrealnameoffile(char ** cpp )
  288. #else
  289. static void packrealnameoffile( cpp )
  290. char ** cpp;
  291. #endif
  292. {
  293.     register char *p, *realname;
  294.  
  295.     realname = realnameoffile;
  296.     if ((int)(p = *cpp)) {
  297.     while ((*p != ':') && *p) {
  298.         *realname++ = *p++;
  299.         if (realname == &(realnameoffile[filenamesize-1]))
  300.         break;
  301.     }
  302.     if (*p == '¥0') *cpp = NULL;
  303.     else *cpp = p+1;
  304.     *realname++ = '/';
  305.     }
  306.     p = nameoffile;
  307.     while (*p != ' ') {
  308.     if (realname >= &(realnameoffile[filenamesize-1])) {
  309.         FPrintFStdErr( "! Full file name is too long¥n");
  310.         break;
  311.     }
  312.     *realname++ = *p++;
  313.     }
  314.     *realname = '¥0';
  315. }
  316.  
  317. extern void main_body(void);
  318.  
  319. #undef __profile__
  320.  
  321. #ifdef __profile__
  322. #include <profiler.h>
  323. #endif
  324.  
  325. void vince_main(void){
  326.     extern FileDescPtr myBufferedFile;
  327.     myBufferedFile = 0;
  328.     setpaths();
  329.     main_body();
  330. #ifdef __profile__
  331.     ProfilerDump("¥pbibprof");
  332.     ProfilerTerm();
  333. #endif
  334. }
  335.  
  336. unsigned long GetDirIDFromFSSpec(FSSpecPtr pFSS)
  337. {
  338.     CInfoPBRec      myCPB;          /* for the PBGetCatInfo call */
  339.     OSErr           err;
  340.     FSSpec          localFSS;
  341.     
  342.     // copy the FSS, so we don't change it:
  343.     BlockMove(pFSS, &localFSS, sizeof(FSSpec));
  344.  
  345.     myCPB.hFileInfo.ioNamePtr = localFSS.name;
  346.     myCPB.hFileInfo.ioVRefNum = localFSS.vRefNum;
  347.     myCPB.hFileInfo.ioDirID = localFSS.parID;
  348.     myCPB.hFileInfo.ioFDirIndex = 0;
  349.     
  350.     err = PBGetCatInfoSync(&myCPB);
  351.     if ((myCPB.hFileInfo.ioFlAttrib & ioDirMask) != 0)
  352.         return myCPB.hFileInfo.ioDirID; // we have a directory, return the dirID
  353.     else
  354.         return localFSS.parID;  // for a file, return the parent ID.
  355. }
  356.  
  357.                                         /* no exit = LocalExit in here */
  358. #undef exit
  359.  
  360. #ifdef STDC
  361. void LocalExit(int i)
  362. #else
  363. void LocalExit(i)
  364.     int i;
  365. #endif
  366. {
  367.     //Click_On( i!=0);            /* only click for errors */
  368.     exit(i);
  369. }
  370.  
  371. FSSpec getAppSpec(void) {
  372.      OSErr myErr    ;
  373.      ProcessInfoRec myProInfo    ;
  374.      ProcessSerialNumber myPSN    ;
  375.      FSSpec FS ;
  376.  
  377.   myPSN.highLongOfPSN = 0;
  378.   myPSN.lowLongOfPSN =    kCurrentProcess;
  379.  
  380.     myProInfo.processInfoLength = sizeof(ProcessInfoRec);
  381.     myProInfo.processName    = nil;
  382.     myProInfo.processAppSpec = &FS;
  383.  
  384.     myErr    = GetProcessInformation(&myPSN,    &myProInfo);
  385.  
  386.     return FS;
  387. }
  388.  
  389.  
  390.